/*
 * EditPatientDialog.java
 *
 * Created on __DATE__, __TIME__
 */

package ui;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import bean.ConnectionBean;

/**
 *
 * @author  __USER__
 */
public class EditPatientDialog extends javax.swing.JDialog
{
	
	/** Creates new form EditPatientDialog */
	public EditPatientDialog(javax.swing.JFrame parent, boolean modal, PatientList parentInternalFrame)
	{
		super(parent, modal);
		this.parentInternalFrame = parentInternalFrame;
		initComponents();
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{
		
		idDOB = new net.sf.nachocalendar.components.DateField();
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		sdf.setLenient(false);
		idDOB.setDateFormat(sdf);
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		idName = new javax.swing.JTextField();
		idPhone = new javax.swing.JTextField();
		idPlace = new javax.swing.JTextField();
		jScrollPane1 = new javax.swing.JScrollPane();
		idAddress = new javax.swing.JTextArea();
		idSex = new javax.swing.JComboBox();
		jLabel7 = new javax.swing.JLabel();
		idUpdate = new javax.swing.JButton();
		
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		
		jLabel1.setText("Name");
		
		jLabel2.setText("Date of Birth");
		
		jLabel3.setText("Phone Number");
		
		jLabel4.setText("Place");
		
		jLabel5.setText("Address");
		
		jLabel6.setText("Sex");
		
		idAddress.setColumns(20);
		idAddress.setLineWrap(true);
		idAddress.setRows(5);
		idAddress.setWrapStyleWord(true);
		jScrollPane1.setViewportView(idAddress);
		
		idSex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "M", "F" }));
		
		jLabel7.setText("Update Patient Details");
		
		idUpdate.setText("Update");
		idUpdate.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				idUpdateActionPerformed(evt);
			}
		});
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
								layout.createSequentialGroup().addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
												layout.createSequentialGroup().addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 177,
														javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
														javax.swing.LayoutStyle.ComponentPlacement.RELATED, 199,
														javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(
												layout.createSequentialGroup().addGroup(
														layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel5).addComponent(
																jLabel4).addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 116,
																Short.MAX_VALUE).addComponent(jLabel2).addComponent(jLabel1)).addPreferredGap(
														javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
														layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
																jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE).addComponent(
																idPhone, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE).addComponent(
																idDOB, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE).addComponent(
																idPlace, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE).addComponent(
																idName, javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)).addPreferredGap(
														javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel6).addPreferredGap(
														javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(idSex,
														javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))).addGap(11, 11, 11)).addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(idUpdate).addContainerGap()))));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addComponent(jLabel7).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel6).addComponent(idSex,
										javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(idName, javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addComponent(jLabel1)).addGap(
						13, 13, 13).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(idDOB,
								javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel2)).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
								layout.createSequentialGroup().addComponent(idPhone, javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(idPlace,
										javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(
								layout.createSequentialGroup().addComponent(jLabel3).addGap(18, 18, 18).addComponent(jLabel4))).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel5).addComponent(jScrollPane1,
								javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(idUpdate).addContainerGap(21, Short.MAX_VALUE)));
		
		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	private void idUpdateActionPerformed(java.awt.event.ActionEvent evt)
	{
		Connection con = ConnectionBean.getConnection();
		try
		{
			if (!idPhone.getText().trim().equalsIgnoreCase(oldPhoneNumber.trim()))
			{
				PreparedStatement psm = con.prepareStatement("select * from patient where phone = ?");
				psm.setString(1, idPhone.getText().trim());
				ResultSet rs = psm.executeQuery();
				if (rs.next()) JOptionPane.showMessageDialog(this, "Please Use a different phone number, this number is already in use by "
						+ rs.getString("name"));
				else
				{
					updatePatient();
				}
			}
			else
			{
				updatePatient();
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void updatePatient() throws Exception
	{
		Connection con = ConnectionBean.getConnection();
		PreparedStatement statement =
				con.prepareStatement("update patient set name = ?, DOB = ?, phone = ?,  address = ?, place = ?, sex = ? where phone = ?");
		statement.setString(1, idName.getText().trim());
		statement.setDate(2, new java.sql.Date(new SimpleDateFormat("dd-MM-yyyy").parse(idDOB.getFormattedTextField().getText().trim()).getTime()));
		statement.setString(3, idPhone.getText().trim());
		statement.setString(4, idAddress.getText().trim());
		statement.setString(5, idPlace.getText().trim());
		statement.setString(6, idSex.getSelectedItem().toString());
		statement.setString(7, oldPhoneNumber);
		int i = statement.executeUpdate();
		String message = "";
		if (i == 1) message = "Updated Successfully";
		else message = "Not Updated. You may contact Rituraj " + i;
		JOptionPane.showMessageDialog(this, message);
		this.dispose();
		this.parentInternalFrame.fillTableData();
		
	}
	
	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	public javax.swing.JTextArea idAddress;
	public net.sf.nachocalendar.components.DateField idDOB;
	public javax.swing.JTextField idName;
	public javax.swing.JTextField idPhone;
	public javax.swing.JTextField idPlace;
	public javax.swing.JComboBox idSex;
	private javax.swing.JButton idUpdate;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JScrollPane jScrollPane1;
	// End of variables declaration//GEN-END:variables
	public String oldPhoneNumber = "";
	private PatientList parentInternalFrame;
}
